home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / clients / listres / listres.c < prev    next >
C/C++ Source or Header  |  1994-08-12  |  9KB  |  299 lines

  1. /*
  2.  * $XConsortium: listres.c,v 1.31 91/02/16 18:30:40 dave Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Jim Fulton, MIT X Consortium
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <X11/Xos.h>
  28. #include <X11/StringDefs.h>
  29. #ifdef MSDOS
  30. #include "X11/IntrinsP.h"      /* QDK 05/11/1994  2:58pm. */
  31. #else
  32. #include "X11/IntrinsicP.h"
  33. #endif
  34. #include <X11/Xaw/Cardinals.h>
  35. #include <X11/Core.h>
  36. #include <X11/Xmu/CharSet.h>
  37. #include <X11/Xmu/WidgetNode.h>
  38. #include <X11/Xaw/AllWidgets.h>
  39.  
  40. #define widget_list XawWidgetArray  /* or motif or ol or ... */
  41. #define nwidgets XawWidgetCount
  42.  
  43.  
  44. static XrmOptionDescRec Options[] = {
  45.   { "-top", "*topObject", XrmoptionSepArg, (caddr_t) NULL },
  46.   { "-format", "*resourceFormat", XrmoptionSepArg, (caddr_t) NULL },
  47.   { "-tree", "*showTree", XrmoptionNoArg, (caddr_t) "on" },
  48.   { "-nosuper", "*showSuper", XrmoptionNoArg, (caddr_t) "off" },
  49.   { "-variable", "*showVariable", XrmoptionNoArg, (caddr_t) "on" },
  50. };
  51.  
  52. typedef struct {
  53.     Boolean show_tree;
  54.     Boolean show_all;
  55.     Boolean show_variable;
  56.     Boolean show_superclass;
  57.     char *top_object;
  58.     char *format;
  59.   } OptionsRec;
  60.  
  61. OptionsRec options;
  62.  
  63. #define Offset(field) XtOffsetOf(OptionsRec, field)
  64.  
  65. static XtResource Resources[] = {
  66.   { "showTree", "ShowTree", XtRBoolean, sizeof(Boolean),
  67.       Offset(show_tree), XtRImmediate, (XtPointer) FALSE },
  68.   { "showSuper", "ShowSuper", XtRBoolean, sizeof(Boolean),
  69.       Offset(show_superclass), XtRImmediate, (caddr_t) TRUE },
  70.   { "showVariable", "ShowVariable", XtRBoolean, sizeof(Boolean),
  71.       Offset(show_variable), XtRImmediate, (caddr_t) FALSE },
  72.   { "topObject", "TopObject", XtRString, sizeof(char *),
  73.       Offset(top_object), XtRString, (caddr_t) "core" },
  74.   { "resourceFormat", "ResourceFormat", XtRString, sizeof(char *),
  75.       Offset(format), XtRString, (caddr_t) " %-16s %20s  %-20s  %s" },
  76. };
  77.  
  78. #undef Offset
  79.  
  80. char *ProgramName;
  81.  
  82. usage ()
  83. {
  84.     fprintf(stderr, "usage:  %s [-options...]\n", ProgramName);
  85.     fprintf(stderr, "\nwhere options include:\n");
  86.     fprintf(stderr,
  87.         "    -all             list all known widget and object classes\n");
  88.     fprintf(stderr,
  89.         "    -tree            list all widgets and objects in a tree\n");
  90.     fprintf(stderr,
  91.         "    -nosuper         do not print superclass resources\n");
  92.     fprintf(stderr,
  93.         "    -variable        show variable name instead of class name\n");
  94.     fprintf(stderr,
  95.         "    -top name        object to be top of tree\n");
  96.     fprintf(stderr,
  97.         "    -format string   printf format for instance, class, type\n");
  98.     fprintf(stderr, "\n");
  99.     exit (1);
  100. }
  101.  
  102. static void print_tree_level (wn, level)
  103.     register XmuWidgetNode *wn;
  104.     register int level;
  105. {
  106.     register int i;
  107.  
  108.     if (!wn) return;
  109.  
  110.     for (i = 0; i < level; i++) {
  111.     putchar (' '); putchar (' '); 
  112.     }
  113.     printf ("%d:  %s/%s\n", level, wn->label, XmuWnClassname(wn));
  114.     print_tree_level (wn->children, level + 1);
  115.     print_tree_level (wn->siblings, level);
  116. }
  117.  
  118. static void tree_known_widgets ()
  119. {
  120.     register int i;
  121.     register XmuWidgetNode *wn;
  122.  
  123.     for (i = 0, wn = widget_list; i < nwidgets; i++, wn++) {
  124.     if (!wn->superclass) {        /* list all rooted objects */
  125.         print_tree_level (wn, 0);
  126.     }
  127.     }
  128. }
  129.  
  130.  
  131. /*
  132.  * print_classname - print out the superclass-to-subclass hierchy of names
  133.  * in the form super\sub\sub....
  134.  */
  135. static int print_classname (node, topnode, level, showvar)
  136.     XmuWidgetNode *node, *topnode;
  137.     int level;
  138.     Bool showvar;
  139. {
  140.     int retval;
  141.  
  142.     if (node && node != topnode) {
  143.     retval = print_classname (node->superclass, topnode, level + 1,
  144.                   showvar);
  145.     } else {
  146.     retval = level - 1;
  147.     }
  148.     if (node)
  149.       printf ("%s%s", showvar ? node->label : XmuWnClassname(node),
  150.           level ? "\\" : "");
  151.  
  152.     return retval;
  153. }
  154.  
  155. static void list_known_widgets ()
  156. {
  157.     int i;
  158.     XmuWidgetNode *wn;
  159.     int width = 0;
  160.     char format[20];
  161.  
  162.     for (i = 0, wn = widget_list; i < nwidgets; i++, wn++) {
  163.     int l = strlen (wn->label);
  164.     if (l > width) width = l;
  165.     }
  166.     sprintf (format, "%%-%ds  ", width);
  167.     for (i = 0, wn = widget_list; i < nwidgets; i++, wn++) {
  168.     printf (format, wn->label);
  169.     print_classname (wn, (XmuWidgetNode *) NULL, 0, False);
  170.     putchar ('\n');
  171.     }
  172. }
  173.  
  174. /* ARGSUSED */
  175. static void print_resources (node, format, topnode, showsuper, showvar)
  176.     XmuWidgetNode *node;
  177.     char *format;
  178.     XmuWidgetNode *topnode;
  179.     Bool showsuper;
  180.     Bool showvar;
  181. {
  182.     int i;
  183.     XtResourceList res = node->resources;
  184.     XmuWidgetNode **wn = node->resourcewn;
  185.  
  186.     for (i = 0; i < node->nresources; i++, res++, wn++) {
  187.     if (!showsuper && *wn != node) continue;
  188.     printf (format, showvar ? (*wn)->label : XmuWnClassname(*wn),
  189.         res->resource_name, res->resource_class, res->resource_type);
  190.     putchar ('\n');
  191.     }
  192.     if (node->nconstraints > 0) {
  193.     printf (format, "----", "----", "----", "----");
  194.     putchar ('\n');
  195.     }
  196.     res = node->constraints;
  197.     wn = node->constraintwn;
  198.     for (i = 0; i < node->nconstraints; i++, res++, wn++) {
  199.     if (!showsuper && *wn != node) continue;
  200.     printf (format, showvar ? (*wn)->label : XmuWnClassname(*wn),
  201.         res->resource_name, res->resource_class, res->resource_type);
  202.     putchar ('\n');
  203.     }
  204.     return;
  205. }
  206.  
  207.  
  208. /*
  209.  * list_resources - display resources of a widget, identifying class from
  210.  * which they come
  211.  */
  212. static list_resources (node, format, topnode, toplevel, showsuper, showvar)
  213.     XmuWidgetNode *node;
  214.     char *format;
  215.     XmuWidgetNode *topnode;
  216.     Widget toplevel;
  217.     Bool showsuper;
  218.     Bool showvar;
  219. {
  220.     static Bool first = True;
  221.  
  222.     XmuWnFetchResources (node, toplevel, topnode);
  223.     if (first) {
  224.     printf (format, showvar ? "Variable" : "WidgetClass",
  225.         "Instance", "Class", "Type");
  226.     putchar ('\n');
  227.     printf (format, showvar ? "--------" : "-----------",
  228.         "--------", "-----", "----");
  229.     putchar ('\n');
  230.     first = False;
  231.     }
  232.     printf ("%s:  ", node->label);
  233.     print_classname (node, topnode, 0, showvar);
  234.     putchar ('\n');
  235.     print_resources (node, format, topnode, showsuper, showvar);
  236.     putchar ('\n');
  237. }
  238.  
  239.  
  240. main (argc, argv)
  241.     int argc;
  242.     char **argv;
  243. {
  244.     int i;
  245.     XtAppContext appcon;
  246.     XmuWidgetNode *topnode;
  247.     Widget toplevel, container;
  248.  
  249.     ProgramName = argv[0];
  250.  
  251.     toplevel = XtAppInitialize (&appcon, "Listres", Options, XtNumber(Options),
  252.                 &argc, argv, NULL, NULL, 0);
  253.     container = XtCreateWidget ("dummy", widgetClass, toplevel, NULL, ZERO);
  254.  
  255.     XtGetApplicationResources (toplevel, (caddr_t) &options,
  256.                    Resources, XtNumber(Resources), NULL, ZERO);
  257.     XmuWnInitializeNodes (widget_list, nwidgets);
  258.     if (argc == 1) {
  259.     if (options.show_tree) {
  260.         tree_known_widgets();
  261.     } else {
  262.         list_known_widgets();
  263.     }
  264.     exit (0);
  265.     }
  266.  
  267.     topnode = XmuWnNameToNode (widget_list, nwidgets, options.top_object);
  268.     argc--, argv++;            /* skip command */
  269.  
  270.     if (argc > 0 && argv[0][0] == '-') {
  271.     int len = strlen (argv[0]);
  272.     if (len >= 2 && strncmp(argv[0], "-all", len) == 0) {
  273.         XmuWidgetNode *wn;
  274.         for (i = 0, wn = widget_list; i < nwidgets; i++, wn++) {
  275.         list_resources (wn, options.format, topnode, container,
  276.                 (Bool) options.show_superclass,
  277.                 (Bool) options.show_variable);
  278.         }
  279.     } else
  280.       usage();
  281.     } else {
  282.     for (; argc > 0; argc--, argv++) {
  283.         XmuWidgetNode *node;
  284.  
  285.         if (argv[0][0] == '-') usage ();
  286.         node = XmuWnNameToNode (widget_list, nwidgets, *argv);
  287.         if (!node) {
  288.         fprintf (stderr, "%s:  unable to find widget \"%s\"\n",
  289.              ProgramName, *argv);
  290.         continue;
  291.         }
  292.         list_resources (node, options.format, topnode, container,
  293.                 (Bool) options.show_superclass,
  294.                 (Bool) options.show_variable);
  295.     }
  296.     }
  297.     exit (0);
  298. }
  299.